package com.jagochan.module.springai.tools;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.jagochan.module.springai.pojo.po.Course;
import com.jagochan.module.springai.pojo.po.CourseReservation;
import com.jagochan.module.springai.pojo.po.School;
import com.jagochan.module.springai.pojo.query.CourseQuery;
import com.jagochan.module.springai.service.CourseReservationService;
import com.jagochan.module.springai.service.CourseService;
import com.jagochan.module.springai.service.SchoolService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@RequiredArgsConstructor
@Component
public class CourseTools {

    private final CourseService courseService;
    private final SchoolService schoolService;
    private final CourseReservationService courseReservationService;

    @Tool(description = "根据条件查询课程")
    public List<Course> queryCourse(@ToolParam(required = false, description = "课程查询条件") CourseQuery query) {
        if (Objects.isNull(query)) {
            return courseService.list();
        }

        QueryChainWrapper<Course> wrapper = courseService.query();
        wrapper.eq(query.getType() != null, "type", query.getType()) // 课程类型：编程、设计、自媒体、其它
                .le(query.getEdu() != null, "edu", query.getEdu()); // 学历要求
        if (query.getSorts() != null) { // 排序
            for (CourseQuery.Sort sort : query.getSorts()) {
                wrapper.orderBy(true, sort.getAsc(), sort.getField());
            }
        }
        return wrapper.list();
    }

    @Tool(description = "查询所有校区")
    public List<School> queryAllSchools() {
        return schoolService.list();
    }

    @Tool(description = "生成课程预约单, 并返回生成的预约单号")
    public String generateCourseReservation(
            @ToolParam(description = "预约课程名") String courseName,
            @ToolParam(description = "学生姓名") String studentName,
            @ToolParam(description = "联系方式") String contactInfo,
            @ToolParam(description = "预约校区") String school,
            @ToolParam(description = "备注") String remark) {

        CourseReservation po = CourseReservation.builder()
                .course(courseName)
                .studentName(studentName)
                .contactInfo(contactInfo)
                .school(school)
                .remark(remark)
                .build();

        // 插入数据库
        courseReservationService.save(po);

        // 返回预约单号
        return String.valueOf(po.getId());
    }
}